---
# create a new taskotron CI stg server
# NOTE: make sure there is room/space for this server on the vmhost
# NOTE: most of these vars_path come from group_vars/mirrorlist or from hostvars

- import_playbook: "/srv/web/infra/ansible/playbooks/include/virt-create.yml myhosts=qa-prod:qa-stg"

- name: make the box be real
  hosts: qa-prod:qa-stg
  user: root
  gather_facts: True

  vars_files:
   - /srv/web/infra/ansible/vars/global.yml
   - "/srv/private/ansible/vars.yml"
   - /srv/web/infra/ansible/vars/{{ ansible_distribution }}.yml

  roles:
   - { role: base, tags: ['base'] }
   - { role: rkhunter, tags: ['rkhunter'] }
   - { role: nagios_client, tags: ['nagios_client'] }
   - hosts
   - { role: fas_client, tags: ['fas_client'] }
   - { role: collectd/base, tags: ['collectd_base'] }
   - { role: sudo, tags: ['sudo'] }
   - { role: openvpn/client,
       when: deployment_type != "qa-stg", tags: ['openvpn_client'] }
   - apache

  pre_tasks:
  - import_tasks: "{{ tasks_path }}/yumrepos.yml"

  tasks:
  # this is how you include other task lists
  - import_tasks: "{{ tasks_path }}/2fa_client.yml"
  - import_tasks: "{{ tasks_path }}/motd.yml"

  handlers:
   - import_tasks: "{{ handlers_path }}/restart_services.yml"

- name: configure phabricator
  hosts: qa-prod:qa-stg
  user: root

  gather_facts: True

  vars_files:
   - /srv/web/infra/ansible/vars/global.yml
   - "/srv/private/ansible/vars.yml"
   - /srv/web/infra/ansible/vars/{{ ansible_distribution }}.yml

  roles:
   - { role: mariadb_server, tags: ['mariadb'] }
   - { role: phabricator, tags: ['phabricator'] }

  handlers:
   - import_tasks: "{{ handlers_path }}/restart_services.yml"


- name: configure qa buildbot CI
  hosts: qa-stg
  user: root
  gather_facts: True

  vars_files:
   - /srv/web/infra/ansible/vars/global.yml
   - "/srv/private/ansible/vars.yml"
   - /srv/web/infra/ansible/vars/{{ ansible_distribution }}.yml

  roles:
   - { role: taskotron/buildmaster, tags: ['buildmaster'] }
   - { role: taskotron/buildmaster-configure, tags: ['buildmasterconfig'] }
   - { role: taskotron/buildslave, tags: ['buildslave'] }
   - { role: taskotron/buildslave-configure, tags: ['buildslaveconfig'] }

  handlers:
   - import_tasks: "{{ handlers_path }}/restart_services.yml"

- name: configure static sites for qa-stg
  hosts: qa-prod:qa-stg
  user: root
  gather_facts: True

  vars_files:
   - /srv/web/infra/ansible/vars/global.yml
   - "/srv/private/ansible/vars.yml"
   - /srv/web/infra/ansible/vars/{{ ansible_distribution }}.yml

  tasks:
    - name: ensure ServerName is set in httpd.conf
      replace: dest=/etc/httpd/conf/httpd.conf regexp='^#ServerName .*$' replace='ServerName {{ external_hostname }}:443'
      notify:
        - reload httpd
      tags:
        - qastaticsites

    - name: create dirs for static sites
      file: path={{ item.document_root }} state=directory owner=apache group=apache mode=1755 setype=httpd_sys_content_t
      with_items: "{{ static_sites }}"
      tags:
        - qastaticsites

    - name: generate virtualhosts for static sites
      template:  src={{ files }}/httpd/qadevel-virtualhost.conf.j2 dest=/etc/httpd/conf.d/{{ item.name }}.conf owner=root group=root mode=0644
      with_items: "{{ static_sites }}"
      notify:
        - reload httpd
      tags:
        - qastaticsites

# don't need this if buildbot is not enabled
#  roles:
#   - { role: taskotron/imagefactory-client,
#       when: deployment_type != "qa-stg", tags: ['imagefactoryclient'] }
#
  handlers:
     - import_tasks: "{{ handlers_path }}/restart_services.yml"


